package com.yjd.comm.base.control;

import com.yjd.comm.user.dto.MerchantSession;
import com.yjd.comm.user.enums.ClientChannelEnum;
import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.vo.CachedKeys;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 商户基础control
 * Created by yangchangyan on 2017/8/16.
 */
public class MerchantBaseControl extends BaseControl {

    /**
     * 获取商户
     * Created by yangchangyan on 2017/8/16.
     *
     * @param
     * @return
     * @throws
     */
    protected MerchantSession getMerchantSession() {
        return getMerchantInfo(request, response, ClientChannelEnum.MOBILE);
    }

    /**
     * 获取商户id
     * @return
     */
    protected Long getMerchantId() {
        return getMerchantInfo(request, response, ClientChannelEnum.MOBILE).getOrg_id();
    }

    private MerchantSession getMerchantInfo(HttpServletRequest request, HttpServletResponse response, ClientChannelEnum clientChannelEnum) {
        MerchantSession merchantSession = (MerchantSession) request.getAttribute(CachedKeys.MERCHANT_USER);
        if (clientChannelEnum == ClientChannelEnum.PIC) {
            if (merchantSession == null) {
                merchantSession = (MerchantSession) ServletUtil.getSession(request,
                        response, CachedKeys.MERCHANT_USER);
            }
        }
        return merchantSession;
    }


    @ModelAttribute
    public void setReqAndResp(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

}
